package mathgame.core;

import java.util.ArrayList;
import java.util.Random;

import javax.swing.JOptionPane;

import mathgame.core.Chapter.ChapterNames;

public class Round {
    private int ans;
    public Boolean correctAns;
    private int solution;
    private Score score;
    private ProblemGenerator problemGenerator;
    
    public String answerFeedback = "";
    
	public void setAnswer(String answer){
		ans = Integer.parseInt(answer);
	}
	
	public Round(ArrayList<Chapter> chapters) {
		problemGenerator = new ProblemGenerator(chapters);
        score = new Score();
        correctAns = false;
	}

    public void correctTextFile(String answer){
        if(answer.equals(problemGenerator.problemSolution)){
            answerFeedback =  "Correct! Well done!";
        }
        else 
        	try {
        		setAnswer(answer);
        	}
        	catch (NumberFormatException badInput) {
                JOptionPane.showMessageDialog(null, "The value must be a numeric value. " );
            }
        	if (ans == problemGenerator.solution){
        		answerFeedback = "Correct! Well done!";
        	}
        	else if (problemGenerator.chapter.name == ChapterNames.EQUATION || problemGenerator.chapter.name == ChapterNames.PERCENT){
            answerFeedback = "Sorry, the correct answer is  " + problemGenerator.problemSolution;
        	}
        	else
        		answerFeedback = "Sorry, the correct answer is  " + problemGenerator.solution;
    }
	public String getProblem() {
		return problemGenerator.generateProblem();
	}

    public Boolean correct(){
    	solution = problemGenerator.solution;
    	
         if(ans == solution){
            score.answerCorrect();
            answerFeedback = "Correct!";
            correctAns = true;
         }
        else {
        	score.comboLost();
        	answerFeedback = "Wrong! :(";
            correctAns = false;
         }
         return correctAns;
    }

    public Score getScore() {
        return score;
    }
   
}
